//
//  mutlichoiceViewController.m
//  PhyloHunter
//
//  Created by Chris Hamilton on 7/15/13.
//  Copyright (c) 2013 The Gigglers. All rights reserved.
//
//	Programmer(s): Chris Hamilton
//
//	Changes: N/A
//	Bugs: N/A
//
//	Task:	Is the View Controller for the 'multi choice question'

#import "mutlichoiceViewController.h"

@implementation mutlichoiceViewController

@synthesize serverURL, Question, button1, button2, button3, button4, cardID;

xmlparser * parser;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //get the xml for the current question from the server and parse it into parser.currentquestion
	parser = [[xmlparser alloc] loadXMLByURL:serverURL];
    
    //set the UI elements to the values parsed by the xml parser
    [button1 setTitle:parser.currentquestion.answer1 forState:UIControlStateNormal];
    [button2 setTitle:parser.currentquestion.answer2 forState:UIControlStateNormal];
    [button3 setTitle:parser.currentquestion.answer3 forState:UIControlStateNormal];
    [button4 setTitle:parser.currentquestion.answer4 forState:UIControlStateNormal];
    Question.text = parser.currentquestion.text;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)button1:(id)sender
{
    //if the correct answer was on this button, call correctanswer
    if ([parser.currentquestion.correct isEqualToString:@"1"])
    {
        [self correctanswer];
	}
    
    else
    {
        //else call wrong answer
        [self wronganswer];
    }
    
    //seque back to the main menu
    [self performSegueWithIdentifier:@"mainsegue" sender:self];
}

- (IBAction)button2:(id)sender
{
    //if the correct answer was on this button, call correctanswer
    
    if ([parser.currentquestion.correct isEqualToString:@"2"])
    {
        [self correctanswer];
	}
    
    else
    {
        //else call wrong answer
        [self wronganswer];
    }
    //seque back to the main menu
	[self performSegueWithIdentifier:@"mainsegue" sender:self];
}

- (IBAction)button3:(id)sender
{
    //if the correct answer was on this button, call correctanswer
    if ([parser.currentquestion.correct isEqualToString:@"3"])
    {
        [self correctanswer];
	}
    
    else
    {
        //else call wrong answer
        [self wronganswer];
    }
    //seque back to the main menu
	[self performSegueWithIdentifier:@"mainsegue" sender:self];
}

- (IBAction)button4:(id)sender
{
    //if the correct answer was on this button, call correctanswer
    if ([parser.currentquestion.correct isEqualToString:@"4"])
    {
        [self correctanswer];
	}
    
    else
    {
        //else call wrong answer
        [self wronganswer];
    }
    //seque back to the main menu
	[self performSegueWithIdentifier:@"mainsegue" sender:self];
}


- (IBAction)Back:(id)sender {
	[self performSegueWithIdentifier:@"mainsegue" sender:self];
}

-(void)correctanswer
{
    //this function modifies the saved array of unlocked cards to unlock the new card
    
    //load the current array from file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *arrayPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"arrays.out"];
    NSMutableArray * cardUnlocked = [NSMutableArray arrayWithContentsOfFile:arrayPath];
    
    //convert the cardID sent from the scanner to an int
    NSInteger cardInt = [cardID intValue];
    
    //unlock the card at cardInt index
    [cardUnlocked replaceObjectAtIndex:cardInt withObject:@"Unlocked"];
    
    //write the array back to file
    [cardUnlocked writeToFile:arrayPath atomically:(YES)];
    
    //alert user they've successfully unlocked the card
    UIAlertView * alert = [[ UIAlertView alloc] initWithTitle:@"Congratulations!" message:@"You got the correct answer and you unlocked the card." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    
}

-(void)wronganswer
{
    //alert user they got the wrong answer
    UIAlertView * alert = [[ UIAlertView alloc] initWithTitle:@"Try Again!" message:@"You got the wrong answer, please try again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}

@end
